<!DOCTYPE html>
<title>Cache Storage: ignore search with credentials</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../serviceworker/resources/test-helpers.js"></script>
<script>

function remove_query(url_string) {
  return url_string.split('?')[0];
}

function find_request_object(cache, str) {
  return cache.keys()
    .then(function(requests) {
        return requests.find(function(request) {
            if (request.url.indexOf(str) > 0)
              return true;
            return false;
          });
      })
}

promise_test(function(test) {
    var service_worker;
    var script_url = '../resources/ignore-search-with-credentials-worker.js';
    var scope = '../resources/ignore-search-with-credentials-iframe.html';
    var cache;
    var request;
    return caches.delete('ignore-search')
      .then(function() {
          return service_worker_unregister_and_register(
            test, script_url, scope)
        })
      .then(function(registration) {
          service_worker = registration.installing;
          return wait_for_state(test, service_worker, 'activated');
        })
      .then(function(state) {
          return with_iframe(scope);
        })
      .then(function(frame) {
          // The following xhr requests will be cached in service worker.
          return Promise.all([
              frame.contentWindow.xhr('simple.txt?query=test', 'abc', 'def'),
              frame.contentWindow.xhr('simple.txt', 'abc', 'def'),
              frame.contentWindow.xhr(
                  'simple.txt?query_without_credential=test')
            ]);
        })
      .then(function() {
          return caches.open('ignore-search');
        })
      .then(function(c) {
          cache = c;

          // Per the Fetch spec[1] the Request constructor is intended to throw
          // if credentails are present in the URL, but the Cache API still
          // defines matching behavior for credentials. So, we need to make xhr
          // requests with credentials and save them to cache storage on fetch
          // event in service worker. Then we can retrieve a request(including
          // credentials) from the cache storage.
          //
          // [1] https://fetch.spec.whatwg.org/#dom-request
          return find_request_object(cache, 'abc:def');
        })
      .then(function(r) {
          request = r;
          return cache.matchAll(request, { ignoreSearch : true });
        })
      .then(function(results) {
          assert_equals(results.length, 2);
          var expected = remove_query(request.url);
          assert_equals(remove_query(results[0].url), expected);
          assert_equals(remove_query(results[1].url), expected);
          return cache.keys(request, { ignoreSearch : true });
        })
      .then(function(results) {
          assert_equals(results.length, 2);
          var expected = remove_query(request.url);
          assert_equals(remove_query(results[0].url), expected);
          assert_equals(remove_query(results[1].url), expected);
          return cache.delete(request, { ignoreSearch : true });
        })
      .then(function(result) {
          assert_true(result);
          return cache.matchAll(request, { ignoreSearch : true });
        })
      .then(function(results) {
          assert_equals(results.length, 0);
        });
  });

</script>
